function EasyUiDatagridEditCommon() {
	var datagridId = "";
	var editIndex = undefined;
	var fields = new Array();
	var rowsJsonInputId = "";
	
	this.getDataGridId = function() {
		return this.datagridId;
	};
	
	this.setDataGridId = function(datagridId) {
		this.datagridId = datagridId;
	};
	
	this.setFields = function(fields) {
		this.fields = fields;
	};
	
	this.setRowsJsonInputId = function(rowsJsonInputId) {
		this.rowsJsonInputId = rowsJsonInputId;
	};
	
	this.convertRecords2Json = function() {
		var datas = [];
		
		var datagridRows = $(this.datagridId).datagrid('getRows');
		for (var i = 0; i < datagridRows.length; i++) {
			var data = {}; 
			
			for (var j = 0; j < this.fields.length; j++ ) {
				data[this.fields[j]] = datagridRows[i][this.fields[j]];
			}
			
			datas.push(data);
		}
		
		$(this.rowsJsonInputId).val( JSON.stringify(datas) );
	};
	
	this.getEditIndex = function() {
		return this.editIndex;
	};
	
	this.handleSaveRowChange = function() {
		
	};
	
	this.endEditing = function() {
		if (this.editIndex == undefined) { return true; }
	    if ($(this.datagridId).datagrid('validateRow', this.editIndex)){
	    	this.handleSaveRowChange();
	        
	        this.editIndex = undefined;
	        return true;
	    } else {
	        return false;
	    }
	};
	
	this.onClickRow = function(index) {
	    if (this.editIndex != index){
	        if (this.endEditing()){
	            $(this.datagridId).datagrid('selectRow', index)
	                    .datagrid('beginEdit', index);
	            this.editIndex = index;
	        } else {
	            $(this.datagridId).datagrid('selectRow', this.editIndex);
	        }
	    }
	};
	
	this.append = function() {
		/*
		// 如果是空数据，先增加一条空数据再删除，防止出现样式上的问题
		var rows = $(this.datagridId).datagrid('getRows');
		if (rows <= 0) {
			$(this.datagridId).datagrid({
	        	data: [
	        			{productid:'FL-DSH-01',productname:'Manx'}
	        		]
	        	}); 
	        $(this.datagridId).datagrid('deleteRow', ($(this.datagridId).datagrid('getRows').length - 1) );
		}
		*/
		
	    if (this.endEditing()){
	        $(this.datagridId).datagrid('appendRow',{status:'P'});
	        this.editIndex = $(this.datagridId).datagrid('getRows').length-1;
	        $(this.datagridId).datagrid('selectRow', this.editIndex)
	                .datagrid('beginEdit', this.editIndex);
	    }
	};
	
	this.copy = function() {
		if (this.endEditing()){
			var row = $(this.datagridId).datagrid('getSelected');
			if (row){
				var data = {}; 
				
				for (var i = 0; i < this.fields.length; i++ ) {
					data[this.fields[i]] = eval("row." + this.fields[i]);
				}
	    		
	    		$(this.datagridId).datagrid('appendRow', data);
	    		this.editIndex = $(this.datagridId).datagrid('getRows').length-1;
	            $(this.datagridId).datagrid('selectRow', this.editIndex)
	                    .datagrid('beginEdit', this.editIndex);
			}
			
		}
	};
	
	this.removeit = function() {
	    if (this.editIndex == undefined) { return; }
	    $(this.datagridId).datagrid('cancelEdit', this.editIndex)
	            .datagrid('deleteRow', this.editIndex);
	    this.editIndex = undefined;
	};
	
	this.accept = function() {
	    if (this.endEditing()){
	        $(this.datagridId).datagrid('acceptChanges');
	    }
	    this.convertRecords2Json();
	};
	
	this.reject = function() {
	    $(this.datagridId).datagrid('rejectChanges');
	    this.editIndex = undefined;
	};
}